﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Saker.Kernel.Model;
using Saker.Kernel.BLL;

namespace Saker.Web.Boss.Sys.Modules
{
    public partial class ModuleAdd : System.Web.UI.Page
    {
        string cmd, moduleID, parentID;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmd = Request["cmd"];
            moduleID = Request["moduleID"];
            parentID = Request["parentID"];
            if (!IsPostBack)
            {
                bindAuthority();
                if (cmd == "Edit")
                {
                    bindData();
                }
            }
        }

        private void bindData()
        {
            SysModule md = new SysModuleBLL().GetByModuleID(int.Parse(moduleID));
            txtModuleName.Text = md.ModuleName;
            txtModuleTag.Text = md.ModuleTag;
            rblModuleDisabled.SelectedValue = md.ModuleDisabled == false ? "0" : "1";
            rblIsMenu.SelectedValue = md.IsMenu == false ? "0" : "1";
            txtModuleURL.Text = md.ModuleURL;
            txtModuleOrder.Text = md.ModuleOrder.ToString();
            txtModuleDescription.Text = md.ModuleDescription;
            IEnumerable<SysModuleAuthorityList> authorityList = new SysModuleAuthorityListBLL().GetAllByModuleID(int.Parse(moduleID));
            foreach (SysModuleAuthorityList list in authorityList)
            {
                cblAuthority.Items.FindByValue(list.AuthorityTag).Selected = true;
            }
        }
        private void bindAuthority()
        {
            cblAuthority.DataSource = new SysAuthorityDirBLL().GetAll();
            cblAuthority.DataTextField = "AuthorityName";
            cblAuthority.DataValueField = "AuthorityTag";
            cblAuthority.DataBind();
        }
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            SysModule module;
            if (cmd == "Add")
            {
                module = new SysModule();
            }
            else
            {
                module = new SysModuleBLL().GetByModuleID(moduleID == null ? 0 : Convert.ToInt32(moduleID));
            }
            module.ModuleName = txtModuleName.Text;
            module.ParentID = (parentID == null ? 0 : Convert.ToInt32(parentID));
            module.ModuleTag = txtModuleTag.Text;
            module.ModuleDisabled = rblModuleDisabled.SelectedValue == "0" ? false : true;
            module.IsMenu = rblIsMenu.SelectedValue == "0" ? false : true;
            module.ModuleURL = txtModuleURL.Text;
            module.ModuleOrder = int.Parse(txtModuleOrder.Text);
            module.ModuleDescription = txtModuleDescription.Text;
            if (cmd == "Add")
            {
                SysModule instered = new SysModuleBLL().Add(module);
                foreach (ListItem item in cblAuthority.Items)
                {
                    if (item.Selected == true)
                    {
                        SysModuleAuthorityList authorityList = new SysModuleAuthorityList();
                        authorityList.ModuleID = instered.ModuleID;
                        authorityList.AuthorityTag = item.Value;
                        new SysModuleAuthorityListBLL().Add(authorityList);
                    }
                }
            }
            else
            {
                new SysModuleBLL().Update(module);
                new SysModuleAuthorityListBLL().DeleteByModuleID(module.ModuleID);
                foreach (ListItem item in cblAuthority.Items)
                {
                    if (item.Selected == true)
                    {
                        SysModuleAuthorityList authorityList = new SysModuleAuthorityList();
                        authorityList.ModuleID = module.ModuleID;
                        authorityList.AuthorityTag = item.Value;
                        new SysModuleAuthorityListBLL().Add(authorityList);
                    }
                }
            }
        }

        protected void lbtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ModulesList.aspx");
        }
    }
}